package com.fly.demo.web.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fly.demo.web.entity.LoginResult;
import com.fly.demo.web.service.LoginService;

@RestController
public class MyLoginController
{
    @Resource
    private LoginService loginService;
    
    LoginResult loginResult = new LoginResult();
    
    @RequestMapping({"/", "/index"})
    public String index()
    {
        return "/index";
    }
    
    @RequestMapping("/403")
    public LoginResult unauthorizedRole()
    {
        System.out.println("------没有权限-------");
        loginResult.setResult("没有权限");
        return loginResult;
    }
    
    @RequestMapping(value = "/login")
    public LoginResult login(Map<String, Object> map, HttpServletRequest request)
        throws Exception
    {
        System.out.println("login()");
        String userName = request.getParameter("userName");
        String password = request.getParameter("password");
        LoginResult loginResult = loginService.login(userName, password);
        if (loginResult.isLogin())
        {
            return loginResult;
        }
        else
        {
            map.put("msg", loginResult.getResult());
            map.put("userName", userName);
            loginResult.setMap(map);
            return loginResult;
        }
    }
    
    @RequestMapping("/logout")
    public LoginResult logOut()
    {
        loginService.logout();
        loginResult.setResult("退出成功");
        return loginResult;
    }
    
    /**
     * 未登录，shiro应重定向到登录界面，此处返回未登录状态信息由前端控制跳转页面
     * 
     * @return
     */
    @RequestMapping(value = "/unauth")
    public LoginResult unauth()
    {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", "1000000");
        map.put("msg", "未登录");
        loginResult.setMap(map);
        return loginResult;
    }
    
    @RequestMapping(value = "/auth")
    public LoginResult auth()
    {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", "200");
        map.put("msg", "登录");
        loginResult.setMap(map);
        return loginResult;
    }
    
    @RequestMapping(value = "/user/403")
    public LoginResult exce()
    {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", "500");
        map.put("msg", "数据异常");
        loginResult.setMap(map);
        return loginResult;
    }
    
    @RequestMapping(value = "/error/500")
    public LoginResult error()
    {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", "500");
        map.put("msg", "error异常");
        loginResult.setMap(map);
        return loginResult;
    }
}
